<?
    function getIntegration($xml_file) {
        global $integrate;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("integration");
        if ($filho->length > 0) {
            $integrate = $filho->item(0)->getelementsbytagname("integrate");
            return $integrate;
        }
        else return null;
    }

    function getFunction($indice) {
        global $integrate, $f;
        $function = $integrate->item($indice)->getelementsbytagname("function");
        if ($function->length > 0) {
            $f = $function->item(0)->nodeValue;
            return $f;
        }
        else return null;
    }

    function getLimit($indice) {
        global $integrate, $limit;
        $limite = $integrate->item($indice)->getelementsbytagname("limit");
        if ($limite->length > 0) {
            for($i = 0; $i < $limite->length; $i++) {
                $a = $limite->item($i)->getelementsbytagname("a");
                $limit[2*$i] = $a->item(0)->nodeValue;
                $b = $limite->item($i)->getelementsbytagname("b");
                $limit[2*$i+1] = $b->item(0)->nodeValue;
            }
            return $limit;
        }
        else return null;
    }

    function getPartitions($indice) {
        global $integrate, $partt;
        $partitions = $integrate->item($indice)->getelementsbytagname("partitions");
        if ($partitions->length > 0) {
            for($i = 0; $i < $partitions->length; $i++) {
                $n = $partitions->item($i)->getelementsbytagname("n");
                $partt[2*$i] = $n->item(0)->nodeValue;
                $m = $partitions->item($i)->getelementsbytagname("m");
                if ($m->length > 0) {
                $partt[2*$i+1] = $m->item(0)->nodeValue;
                }
            }
            return $partt;
        }
        else return null;
    }

    function getDesc($indice) {
        global $integrate, $desc;
        $descricao = $integrate->item($indice)->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $integrate, $com;
        $comentario = $integrate->item($indice)->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $integrate, $ref;
        $referencia = $integrate->item($indice)->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function createMenu() {
        global $integrate,$msg;
        for($i = 0; $i < $integrate->length; $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            $temp = getFunction($i); $temp1 = $i+1;
            echo $msg->get('XML_EXIBE_INTNUM_MSG01')." "."$temp1".": &nbsp f(x) = ";
            echo "$temp";
            echo "</u></a><p>";
        }
        return;
    }

    if (empty($xml_file) && empty($id_bib)) $id_bib="bib_demo.xml";
    if ( (strlen(@$indice)==0) || ($indice == null) ) { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=intnum3.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getIntegration($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
        } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
        }
    ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
        </form>
<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {

               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=intnum3.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getIntegration($xml_file); ?>
        <? $funcao = getFunction($indice);?>
        <? $lim = getLimit($indice);?>
        <? if (!$lim==null) {
             $a = $lim[0];
             $b = $lim[1];
           } ?>
        <? $part = getPartitions($indice);?>
        <? if (!$part==null) {
             $n = $part[0];
             if (!empty($part[1])) {
                $m = $part[1];
             }
           } ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>

        <? verificaBib(@$xml_file); ?>
        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b> <? echo $msg->get('XML_EXIBE_INTNUM_MSG02'); ?> </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        
 		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTNUM_MSG03'); ?></font>
		<p align="left"><font face="Arial">f(x) = </font>
	   	<input type="text" size='60' maxlength='100' name="funcao" value="<? echo $funcao ?>">
		<a href=javascript:winopen('ajuda/funcoesphp.php?idioma=PT_BR.php')><font face="Arial"><? echo $msg->get('XML_EXIBE_INTNUM_MSG08'); ?></font></a>
		<p>
  		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTNUM_MSG04'); ?> </font>
           	<input type="text" size='5' maxlength='5' name="a" value="<?echo $a?>">
		<p>
  		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTNUM_MSG05'); ?> </font>
           	<input type="text" size='5' maxlength='5' name="b" value="<?echo $b?>">
		<p>
  		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_INTNUM_MSG06'); ?> </font>
           	<input type="text" size='5' maxlength='5' name="n" value="<?echo $n?>">
		<p>

        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="Calcular" value="sim">
	<input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG011'); ?> onClick="history.back()">
        </form>
	<?
	/*********************************************/
	/* Navegação entre os exemplos da biblioteca */
	/*********************************************/
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=intnum3.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo $msg->get('Outros_Exemplos')."<br>";
    $objeto = new DOMDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("integration");
	if ($filho->length > 0)
	{
		$integrate = $filho->item(0)->getelementsbytagname("integrate");
		$j = 0;
		for ($i = 0; $i < $integrate->length; $i++)
		{
			if ($j != 0) echo " | ";
			if ($j == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($j)\"><u>".($j+1)."</u></a>";
			if ($j == $indice) echo "</b>";
			$j++;
        }
	}
	?>

<? } ?>
